package com.mediacloud.app.newsmodule.fragment.works

import android.os.Bundle
import android.util.TypedValue
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.eventTrack.CDPTrackReportManager
import com.flyco.tablayout.listener.OnTabSelectListener
import com.mediacloud.app.model.activity.BaseBackActivity
import com.mediacloud.app.newsmodule.R
import kotlinx.android.synthetic.main.activity_works.*

/**
 * 我的作品
 */
class MyWorksActivity :BaseBackActivity(){

    val draftBoxFragment by lazy {
        MyDraftBoxFragment()
    }
    override fun getLayoutResID(): Int {
        return R.layout.activity_works
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setTitle("我的作品")
        var titles = arrayOf("草稿箱","提交库")
        val fragments = arrayListOf<Fragment>()
        fragments.add(draftBoxFragment)
        fragments.add(SubmitLibraryFragment())
        vp.adapter = object :FragmentPagerAdapter(supportFragmentManager){
            override fun getCount(): Int {
                return fragments.size
            }

            override fun getItem(position: Int): Fragment {
                return fragments[position]
            }
        }
        tab.setOnTabSelectListener(object :OnTabSelectListener{
            override fun onTabSelect(position: Int) {
                if (position == 0)
                    mMoreText?.visibility = View.VISIBLE
                else
                    mMoreText?.visibility = View.GONE

                titles.forEachIndexed { index, _ ->
                    if (index == position)
                        tab.getTitleView(position).setTextSize(TypedValue.COMPLEX_UNIT_SP, 17f)
                    else
                        tab.getTitleView(index).setTextSize(TypedValue.COMPLEX_UNIT_SP, 15f)

                }
            }

            override fun onTabReselect(position: Int) {

            }

        })
        tab.setViewPager(vp,titles)
        tab.getTitleView(0).setTextSize(TypedValue.COMPLEX_UNIT_SP, 17f)
        vp.offscreenPageLimit = titles.size

        setMoreText("编辑")
        mMoreText?.visibility = View.VISIBLE

        vp.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                CDPTrackReportManager.getInstance().MyContributeListView(titles[position])
                if (position == 0)
                    mMoreText?.visibility = View.VISIBLE
                else
                    mMoreText?.visibility = View.GONE

            }

            override fun onPageScrollStateChanged(state: Int) {
            }

        })
        CDPTrackReportManager.getInstance().MyContributeListView(titles[0])
    }

    override fun moreClicked() {
        super.moreClicked()
        if (mMoreText.text == "编辑"){
            draftBoxFragment.showClearLayout(true)
            mMoreText.text = "取消"
        }else{
            draftBoxFragment.showClearLayout(false)
            mMoreText.text = "编辑"
        }
    }

}